// ComControlATL.h : Declaration of the CComControlATL
#pragma once
#include "resource.h"       // main symbols
#include <atlctl.h>
#include "ComInteropATL_i.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;

//template <class T>
//class ATL_NO_VTABLE IOleControlListenAndDelegateImpl : public IOleControl
//{
//public:
//
//};



// CComControlATL
class ATL_NO_VTABLE CComControlATL :
	public CComObjectRootEx<CComSingleThreadModel>,
	public IDispatchImpl<IComControlATL, &IID_IComControlATL, &LIBID_ComInteropATLLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IPersistStreamInitImpl<CComControlATL>,
	public IOleControlImpl<CComControlATL>,
	public IOleObjectImpl<CComControlATL>,
	public IOleInPlaceActiveObjectImpl<CComControlATL>,
	public IViewObjectExImpl<CComControlATL>,
	public IOleInPlaceObjectWindowlessImpl<CComControlATL>,
	public IObjectWithSiteImpl<CComControlATL>,
	public IPersistStorageImpl<CComControlATL>,
	public IQuickActivateImpl<CComControlATL>,
	//public IOleControlListenAndDelegateImpl<CComControlATL>,
#ifndef _WIN32_WCE
	public IDataObjectImpl<CComControlATL>,
#endif
	public CComCoClass<CComControlATL, &CLSID_ComControlATL>,
	public CComControl<CComControlATL>
{
public:
	CContainedWindow m_ctlButton;

#pragma warning(push)
#pragma warning(disable: 4355) // 'this' : used in base member initializer list


	CComControlATL()
		: m_ctlButton(_T("Button"), this, 1)
	{
		m_bWindowOnly = TRUE;
	}

#pragma warning(pop)

DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE |
	OLEMISC_ACTSLIKEBUTTON |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_INSIDEOUT |
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST
)

DECLARE_REGISTRY_RESOURCEID(IDR_COMCONTROLATL)


BEGIN_COM_MAP(CComControlATL)
	COM_INTERFACE_ENTRY(IComControlATL)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(IViewObjectEx)
	COM_INTERFACE_ENTRY(IViewObject2)
	COM_INTERFACE_ENTRY(IViewObject)
	COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
	COM_INTERFACE_ENTRY(IOleInPlaceObject)
	COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
	COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
	COM_INTERFACE_ENTRY(IOleControl)
	COM_INTERFACE_ENTRY(IOleObject)
	COM_INTERFACE_ENTRY(IPersistStreamInit)
	COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
	COM_INTERFACE_ENTRY(IQuickActivate)
	COM_INTERFACE_ENTRY(IPersistStorage)
#ifndef _WIN32_WCE
	COM_INTERFACE_ENTRY(IDataObject)
#endif
	COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()

BEGIN_PROP_MAP(CComControlATL)
	PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
	PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
	// Example entries
	// PROP_ENTRY_TYPE("Property Name", dispid, clsid, vtType)
	// PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()


BEGIN_MSG_MAP(CComControlATL)
	MESSAGE_HANDLER(WM_CREATE, OnCreate)
	MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
	COMMAND_CODE_HANDLER(BN_CLICKED, OnBNClicked)
	CHAIN_MSG_MAP(CComControl<CComControlATL>)
ALT_MSG_MAP(1)
	// Replace this with message map entries for superclassed Button
END_MSG_MAP()
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

	LRESULT OnBNClicked(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO : Add your code here.
		return 0;
	}

	LRESULT OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		LRESULT lRes = CComControl<CComControlATL>::OnSetFocus(uMsg, wParam, lParam, bHandled);
		if (m_bInPlaceActive)
		{
			if(!IsChild(::GetFocus()))
				m_ctlButton.SetFocus();
		}
		return lRes;
	}

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		RECT rc;
		GetWindowRect(&rc);
		rc.right -= rc.left;
		rc.bottom -= rc.top;
		rc.top = rc.left = 0;
		m_ctlButton.Create(m_hWnd, rc);
		return 0;
	}

	STDMETHOD(SetObjectRects)(LPCRECT prcPos,LPCRECT prcClip)
	{
		IOleInPlaceObjectWindowlessImpl<CComControlATL>::SetObjectRects(prcPos, prcClip);
		int cx, cy;
		cx = prcPos->right - prcPos->left;
		cy = prcPos->bottom - prcPos->top;
		::SetWindowPos(m_ctlButton.m_hWnd, NULL, 0,
			0, cx, cy, SWP_NOZORDER | SWP_NOACTIVATE);
		return S_OK;
	}

// IViewObjectEx
	DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

// IComControlATL

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}


	STDMETHOD(GetControlInfo)(LPCONTROLINFO  pCI )
	{
		return E_UNEXPECTED;
	}
	STDMETHOD(OnMnemonic)(LPMSG  pMsg )
	{
		return E_UNEXPECTED;
	}
	STDMETHOD(OnAmbientPropertyChange)(DISPID dispid)
	{
		if (dispid == 100)	// 100
		{
			CComPtr<IOleClientSite> spClientSite;
			GetClientSite(&spClientSite);
			// process the RealtimeStatus change here
			CComQIPtr<IDispatch> spDispClientSite(spClientSite);
			if (spDispClientSite == NULL)
				return E_FAIL;	// could not access OLE Client Site IDispatch interface

			CComVariant vtProperty;
			DISPPARAMS params = { 0, 0, 0, 0 };
			HRESULT hr = spDispClientSite->Invoke(100, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &params, &vtProperty, 0, 0);
			if (FAILED(hr))
				return E_FAIL;	// OLE Client Site doesn't have the RealtimeStatus property

			_ASSERT(vtProperty.vt == VT_I4);
			// value is in 'vtProperty.lVal'
			if((vtProperty.lVal == 0) || (vtProperty.lVal ==2)) // 0 is when kobra deactivate (REALTIME_CLOSE) and 2 is for trader (REALTIME_PAUSE)
			{
				// When deactivate sheet
				//m_spMNC->put_Active(FALSE);
				MessageBox(L"Dectivate",L"Message Box info",MB_OK);

				return S_OK;
			}
			else // 1 is when kobra activate (REALTIME_OPEN) and 3 is for trader (REALTIME_RESUME)
			{
				// When Activate sheet
				//m_spMNC->put_Active(TRUE);
				MessageBox(L"Activate",L"Message Box info",MB_OK);
				return S_OK;
			}
		}
	}

	STDMETHOD(FreezeEvents)(BOOL bFreeze)
	{
		return E_UNEXPECTED;
	}
};

OBJECT_ENTRY_AUTO(__uuidof(ComControlATL), CComControlATL)
